package dk.dashboardFP.interactionElementFramwork.buttons {
	import dk.dashboardFP.interactionElementFramwork.InteractionElementView;
	import dk.dashboardFP.interactionElementFramwork.InteractionElementViewEvent;
	import dk.dashboardFP.interactionElementFramwork.InteractionElement;
	import dk.dashboardFP.interactionElementFramwork.focusHandler.IFocusGroupActionReciver;

	import flash.events.Event;
	import flash.events.KeyboardEvent;
	import flash.ui.Keyboard;

	/**
	 * @author jk
	 */
	public class AbstractButton extends InteractionElement implements IFocusGroupActionReciver {
		private var _intIconIndex : int;		private var _fOnClickCallback : Function;
		private var _oRefrenceObject : Object;

		public function AbstractButton(oIEV : InteractionElementView, oRefrenceObject:Object=null) {
			_oRefrenceObject = oRefrenceObject;
			super(oIEV);
			
			_bOwnsKeyFocus=true;
		}

		override protected function registerBasicViewEvents() : void {
			super.registerBasicViewEvents();
			
			_doIEV.addEventListener(InteractionElementViewEvent.CLICK_VIEW, onViewClicked, false, 0, true);
			_doIEV.addEventListener(InteractionElementViewEvent.KEY_DOWN_VIEW, onKeyDownWhileInFocus, false, 0, true);
		}

		//::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
		// key while in focus
		//::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
		
		private function onKeyDownWhileInFocus(event : InteractionElementViewEvent) : void {			
			if (KeyboardEvent(event.oOriginalEvent).charCode==Keyboard.SPACE || KeyboardEvent(event.oOriginalEvent).charCode==Keyboard.ENTER) {
				dispatchButtonClick(event.oOriginalEvent);
			}
		}
		
		//::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
		// mouse down up
		//::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

		override protected function onMouseDownView(event : InteractionElementViewEvent) : void {
			super.onMouseDownView(event);
			if (!bDisabled) {
				dispatchEvent(new ButtonEvent(ButtonEvent.MOUSE_DOWN, event));
			}
		}

		override protected function onMouseUpView(event : InteractionElementViewEvent) : void {
			super.onMouseUpView(event);
			if (!bDisabled) {
				dispatchEvent(new ButtonEvent(ButtonEvent.MOUSE_UP, event));
			}
		}
		
		//::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
		// main dispatches
		//::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
		
		protected function dispatchButtonClick(oOriginalEvent : Event=null) : void {
			
			//::
			// cancle dispath if... 
			if (bSelected && bDisableOnSelection) {
				return;
			}
			
			dispatchEvent(new ButtonEvent(ButtonEvent.CLICKED, oOriginalEvent, true));
			
			if (_fOnClickCallback!=null) {
				_fOnClickCallback();
			}
		}

		//::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
		// view event handlers
		//::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
		
		private function onViewClicked(event : InteractionElementViewEvent) : void {
			if (!bDisabled) {
				dispatchButtonClick(event.oOriginalEvent);
			}
		}
		
		//::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
		// icon handling
		//::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
		
		public function get intIconIndex() : int { 
			return _intIconIndex; 
		}
		
		public function set intIconIndex(oNewValue:int) : void { 
			_intIconIndex=oNewValue;
		}
		
		//::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
		// focus group action reciever
		//::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
				
		public function doFocusGroupAction() : void {
			dispatchButtonClick();
		}
		
		//::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
		// simple call back
		//::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
		
		public function get fOnClickCallback() : Function { 
			return _fOnClickCallback; 
		}
		
		public function set fOnClickCallback(oNewValue:Function) : void { 
			_fOnClickCallback=oNewValue;
		}
		
		public function get oRefrenceObject() : Object {
			return _oRefrenceObject;
		}
	}
}
